<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 15/10/8
 * Time: 下午3:10
 */
class ProductPackage extends Eloquent
{
    protected $table = "product_package";


    /**
 * @Author:      lyt
 * @DateTime:    2016-01-19 14:53:36
 * @Description: 用于获取一个指定车型 指定产品包的 匹配耗材信息 服务信息
 */
    public static  function getItem($car_model_id,$package_id){
        $ppi=ProductPackItem::where("pack_id",$package_id)->whereIn("type",array(2,3))->where("status",1)->get();
        $supply_arr=$server_arr=$not_pin=array();
        foreach ($ppi as $_ppi) {
            if($_ppi->type==3){//如果是耗材
                $ss_temp=SupplyClassification::where("id",$_ppi->item_id)->where("status",1)->first();
                if($ss_temp){
                    $array=DataSupplyProductItem::getDefault($car_model_id,$_ppi->item_id);
                    if($array){
                        $supply_arr[]=array(
                            "id"=>$array->id,
                            "model"=>$array->model,
                            "brand_id"=>$array->brand_id,
                            "brand_name"=>$array->brand_name,
                            "series_id"=>$array->series_id,
                            "series_name"=>$array->series_name,
                            "subject"=>$array->subject,
                            "subject_name"=>$array->subject_name,
                            "number"=>$array->number,
                            "max_number"=>$array->max_number,
                            "icon"=>$array->icon,
                            "small_desc"=>$array->small_desc,
                            "desc"=>$array->desc,
                            "amount"=>$array->amount,
                            "unit"=>$array->unit,
                            "name"=>$array->name,
                            "is_adapter"=>1
                        );
                    }else{
                        $supply_arr[]=array(
                            "id"=>$ss_temp->id,
                            "model"=>" ",
                            "brand_id"=>0,
                            "brand_name"=>" ",
                            "series_id"=>0,
                            "series_name"=>" ",
                            "subject"=>$ss_temp->id,
                            "subject_name"=>$ss_temp->name,
                            "number"=>0,
                            "max_number"=>0,
                            "icon"=>" ",
                            "small_desc"=>" ",
                            "desc"=>" ",
                            "amount"=>0,
                            "unit"=>" ",
                            "name"=>" ",
                            "is_adapter"=>2

                        );
                    }
                }
            }else{//说明是 服务
                $temp_ser=ServiceProductItem::getItem($_ppi->item_id);
                if($temp_ser){
                    $server_arr[]=array(
                        "id"=>$temp_ser->id,
                        "name"=>$temp_ser->name,
                        "desc"=>$temp_ser->desc,
                        "icon"=>$temp_ser->icon,
                        "work_hours"=>$temp_ser->work_hours,
                        "type"=>$temp_ser->type,
                        "price"=>$temp_ser->price,
                        "is_adapter"=>1
                    );
                }
            }
        }
        return array("supply"=>$supply_arr,"not_pin"=>$not_pin,"server"=>$server_arr);
    }



    /**
     * @Author:      sqq
     * @DateTime:    2018-11-01 14:53:36
     * @Description: 用于获取一个指定车型 指定产品包的 匹配耗材信息 服务信息  C端3.0使用
     */
    public static  function getItemCS5($car_model_id,$package_id){
        $ppi=ProductPackItem::where("pack_id",$package_id)->whereIn("type",array(2,3))->where("status",1)->get();
        $supply_arr=$server_arr=$not_pin=array();
        foreach ($ppi as $_ppi) {
            if($_ppi->type==3){//如果是耗材
                $ss_temp=SupplyClassification::where("id",$_ppi->item_id)->where("status",1)->first();
                if($ss_temp){
                    $array=DataSupplyProductItem::getDefaultC($car_model_id,$_ppi->item_id);
                    if(!is_null($array)){
                        //排序
                        $array = json_decode(json_encode($array),true);
                        array_multisort(array_column($array,'id'),SORT_ASC,$array);
                        foreach($array as $key => $val){
                            if($val['amount'] != 0){
                                $supply_arr[]=array(
                                    "id"=>$val['id'],
                                    "model"=>$val['model'],
                                    "brand_id"=>$val['brand_id'],
                                    "brand_name"=>$val['brand_name'],
                                    "series_id"=>$val['series_id'],
                                    "series_name"=>$val['series_name'],
                                    "subject"=>$val['subject'],
                                    "subject_name"=>$val['subject_name'],
                                    "number"=>$val['number'],
                                    "max_number"=>$val['max_number'],
                                    "icon"=>$val['icon'],
                                    "small_desc"=>$val['small_desc'],
                                    "desc"=>$val['desc'],
                                    "amount"=>$val['amount'],
                                    "unit"=>$val['unit'],
                                    "name"=>$val['name'],
                                    "is_adapter"=>1
                                );
                                break;
                            }
                        }
                    }else{
                        $supply_arr[]=array(
                            "id"=>$ss_temp->id,
                            "model"=>" ",
                            "brand_id"=>0,
                            "brand_name"=>" ",
                            "series_id"=>0,
                            "series_name"=>" ",
                            "subject"=>$ss_temp->id,
                            "subject_name"=>$ss_temp->name,
                            "number"=>0,
                            "max_number"=>0,
                            "icon"=>" ",
                            "small_desc"=>" ",
                            "desc"=>" ",
                            "amount"=>0,
                            "unit"=>" ",
                            "name"=>" ",
                            "is_adapter"=>2
                        );
                    }
                }
            }else{//说明是 服务
                $temp_ser=ServiceProductItem::getItem($_ppi->item_id);
                if($temp_ser){
                    $server_arr[]=array(
                        "id"=>$temp_ser->id,
                        "name"=>$temp_ser->name,
                        "desc"=>$temp_ser->desc,
                        "icon"=>$temp_ser->icon,
                        "work_hours"=>$temp_ser->work_hours,
                        "type"=>$temp_ser->type,
                        "price"=>$temp_ser->price,
                        "is_adapter"=>1
                    );
                }
            }
        }
        return array("supply"=>$supply_arr,"not_pin"=>$not_pin,"server"=>$server_arr);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-11-01 14:53:36
     * @Description: 用于获取一个指定车型 指定产品包的 匹配耗材信息 服务信息  C端3.0使用
     */
    public static  function getItemC($car_model_id,$package_id){
        $ppi=ProductPackItem::where("pack_id",$package_id)->whereIn("type",array(2,3))->where("status",1)->get();
        $supply_arr=$server_arr=$not_pin=array();
        foreach ($ppi as $_ppi) {
            if($_ppi->type==3){//如果是耗材
                $ss_temp=SupplyClassification::where("id",$_ppi->item_id)->where("status",1)->first();
                if($ss_temp){
                    $array=DataSupplyProductItem::getDefaultC($car_model_id,$_ppi->item_id);
                    if(!is_null($array)){
                        foreach($array as $key => $val){
                            if($val->amount != 0){
                                $supply_arr[]=array(
                                    "id"=>$val->id,
                                    "model"=>$val->model,
                                    "brand_id"=>$val->brand_id,
                                    "brand_name"=>$val->brand_name,
                                    "series_id"=>$val->series_id,
                                    "series_name"=>$val->series_name,
                                    "subject"=>$val->subject,
                                    "subject_name"=>$val->subject_name,
                                    "number"=>$val->number,
                                    "max_number"=>$val->max_number,
                                    "icon"=>$val->icon,
                                    "small_desc"=>$val->small_desc,
                                    "desc"=>$val->desc,
                                    "amount"=>$val->amount,
                                    "unit"=>$val->unit,
                                    "name"=>$val->name,
                                    "is_adapter"=>1
                                );
                            }
                        }
                    }else{
                        $supply_arr[]=array(
                            "id"=>$ss_temp->id,
                            "model"=>" ",
                            "brand_id"=>0,
                            "brand_name"=>" ",
                            "series_id"=>0,
                            "series_name"=>" ",
                            "subject"=>$ss_temp->id,
                            "subject_name"=>$ss_temp->name,
                            "number"=>0,
                            "max_number"=>0,
                            "icon"=>" ",
                            "small_desc"=>" ",
                            "desc"=>" ",
                            "amount"=>0,
                            "unit"=>" ",
                            "name"=>" ",
                            "is_adapter"=>2
                        );
                    }
                }
            }else{//说明是 服务
                $temp_ser=ServiceProductItem::getItem($_ppi->item_id);
                if($temp_ser){
                    $server_arr[]=array(
                        "id"=>$temp_ser->id,
                        "name"=>$temp_ser->name,
                        "desc"=>$temp_ser->desc,
                        "icon"=>$temp_ser->icon,
                        "work_hours"=>$temp_ser->work_hours,
                        "type"=>$temp_ser->type,
                        "price"=>$temp_ser->price,
                        "is_adapter"=>1
                    );
                }
            }
        }
        return array("supply"=>$supply_arr,"not_pin"=>$not_pin,"server"=>$server_arr);
    }



    public static  function getItemCNewS5($car_model_id,$package_id){
        $ppi=ProductPackItem::where("pack_id",$package_id)->whereIn("type",array(2,3))->where("status",1)->get();
        $supply_arr=$server_arr=$not_pin=array();
        foreach ($ppi as $_ppi) {
            $ss_temp=SupplyClassification::where("id",$_ppi->item_id)->where("status",1)->first();
            if($ss_temp){
                $array=DataSupplyProductItem::getDefaultC($car_model_id,$_ppi->item_id);
                if(!is_null($array)){
                    foreach($array as $key => $val){
                        if($val->amount != 0){
                            $supply_arr=array(
                                "id"=>$val->id,
                                "model"=>$val->model,
                                "brand_id"=>$val->brand_id,
                                "brand_name"=>$val->brand_name,
                                "series_id"=>$val->series_id,
                                "series_name"=>$val->series_name,
                                "subject"=>$val->subject,
                                "subject_name"=>$val->subject_name,
                                "number"=>$val->number,
                                "max_number"=>$val->max_number,
                                "icon"=>$val->icon,
                                "small_desc"=>$val->small_desc,
                                "desc"=>$val->desc,
                                "amount"=>$val->amount,
                                "unit"=>$val->unit,
                                "name"=>$val->name,
                                "is_adapter"=>1
                            );
                        }
                    }
                }else{
                    $supply_arr=array(
                        "id"=>$ss_temp->id,
                        "model"=>" ",
                        "brand_id"=>0,
                        "brand_name"=>" ",
                        "series_id"=>0,
                        "series_name"=>" ",
                        "subject"=>$ss_temp->id,
                        "subject_name"=>$ss_temp->name,
                        "number"=>0,
                        "max_number"=>0,
                        "icon"=>" ",
                        "small_desc"=>" ",
                        "desc"=>" ",
                        "amount"=>0,
                        "unit"=>" ",
                        "name"=>" ",
                        "is_adapter"=>2
                    );
                }
            }
        }
        return $supply_arr;
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-1-21 11:37:42
     * @Description: 传入保养项目id 数组 获取 默认保养得项目包
     */
    public static function helpDecode($arr,$city_id){
        asort($arr);
        $ppc=ProductPackageCity::where("city_id",$city_id)->where("status",1)->select("pack_id")->get()->toArray();

        $pack=ProductPackage::whereIn("id",$ppc)->where("status",1)->where("subjectids","!=","")->where("place",2)->get();
//         print_r($pack);
//         exit;
        $pack_arr=array();
        foreach ($pack as $_pack) {
            $pack_arr[$_pack->id]=explode(",", $_pack->subjectids);
        }
        return ProductPackage::spiltArr($pack_arr,$arr,array());
    }

     public static function helpDecodeShop($arr,$shop_id){
        asort($arr);
        $ppc=ProductPackageShop::getShopPackage($shop_id);
        $pack=ProductPackage::whereIn("id",$ppc)->where("status",1)->where("subjectids","!=","")->where("place",2)->get();
        $pack_arr=array();
        foreach ($pack as $_pack) {
            $pack_arr[$_pack->id]=explode(",", $_pack->subjectids);
        }
        return ProductPackage::spiltArr($pack_arr,$arr,array());
    }

    /**
     * @Author:      zwh
     * @DateTime:    2016-1-21 11:37:54
     * @Description: 提供数组分割功能  匹配标数组 数组 第二个数组为 要被匹配的数组  第三个是 最终数组
     */
    public static function spiltArr($sarr,$arr,$fina_arr){
//         print_r($sarr);
//         print_r($arr);
//         exit;

        $max_number=1;//相交的最大个数
        $max_arr=null;
        $del_key=-1;
        $arr_length=PHP_INT_MAX;
        foreach ($sarr as $key => $_sarr) {
            $temp_arr=array_intersect($_sarr,$arr);
            if(sizeof($temp_arr)>$max_number){
                $max_number=sizeof($temp_arr);
                $arr_length=sizeof($_sarr);
                $max_arr=$temp_arr;
                $del_key=$key;
            }else if(sizeof($temp_arr)==$max_number&&$arr_length>sizeof($_sarr)){
                $max_number=sizeof($temp_arr);
                $arr_length=sizeof($_sarr);
                $max_arr=$temp_arr;
                $del_key=$key;
            }
        }
        if($del_key==-1){//说明该次匹配已经没有匹配上的 数组
            $fina_arr['not_pi']=$arr;
            return $fina_arr;
        }
        $fina_arr[$del_key]=array("own"=>$sarr[$del_key],"user"=>$max_arr);//结构为 own 是这个包有那个品类  use  这个包用了那些品类  也就是 匹配数组数组内容吗 use是匹配上了那些数组
        $arr=array_diff($arr,$max_arr);
        unset($sarr[$del_key]); //删除已经用过的数组
        if(sizeof($arr)>0){
            return ProductPackage::spiltArr($sarr,$arr,$fina_arr);
        }else{
            return $fina_arr;
        }
    }

    /**
     * @Author:dx
     * @DateTime:2017-04-19 14:09:41
     * @Description: 根据车型 匹配合适的产品包
     *
     * 废弃方法 已经无人掉调用
     */

    public static function getRightPackages($place,$city,$car_model_id){
        $ppc=ProductPackageCity::where("city_id",$city)->where("status",1)->select("pack_id")->get()->toArray();
        $pk = ProductPackage::leftjoin('product_pack_item','product_package.id','=','product_pack_item.pack_id')
            ->where('product_package.status', 1)
            ->where('product_package.place', $place)
            ->whereIn('product_package.id', $ppc)
            ->where('product_package.is_public',1)
            ->where('product_pack_item.status',1)
            ->where('product_pack_item.type',3)
            ->select(array('product_pack_item.item_id','product_package.id'))
            ->get();
        $rp = array();
        foreach($pk as $_p){
            $spi = SupplyProductItem::getRightSupplyProductItem($car_model_id,$_p->item_id);
            $rp[$_p->id][] = $spi;
        }
        return $rp;
    }

    /**
     * @Author:dx
     * @DateTime:    2016-04-27 14:09:41
     * @Description: 根据产品包id 获取产品包信息
     */
    public static function getPP($rp){
        $product_package_data = array();
        $product_classification = DB::table('product_classification')->get();
        foreach($product_classification as $p_c){
            $package = ProductPackage::getppinfo($rp);
            $p = array();
            if(in_array($p_c->id,array_keys($package))){
                $p = $package[$p_c->id];
            }
            $pcd = array(
                'id' => $p_c->id,
                'name' => $p_c->name,
                'product_img' => Config::get('config.product_img'),
                'packages' => $p
            );
            array_push($product_package_data,$pcd);
        }
        return $product_package_data;
    }

    /**
     * @Author:      dx
     * @DateTime:    2017-04-25 14:53:36
     * @Description: 用于获取产品包分类
     */
    public static function getppinfo($rp){
        $pp = array();
        foreach($rp as $key => $_r){
            if(!empty($_r)){
                $pp[] = ProductPackage::where('status',1)->where('place',2)->where('id',$key)->select('name','id','icon','classification','is_checked','small_icon')->first()->toArray();
            }
        }
        $c = array();
        foreach($pp as $p){
            $c[$p['classification']][] = $p;
        }
        return $c;
    }

    /**
     * @Author:      dx
     * @DateTime:    2017-04-25 14:53:36
     * @Description: 用于获取一个指定车型 指定产品包的 匹配耗材信息 服务信息
     */
    public static  function getItemInfo($car_model_id,$package_id){
        $ppi=ProductPackItem::where("pack_id",$package_id)->where("status",1)->get();
        $not_supply_arr=array();
        foreach ($ppi as $_ppi) {
            if($_ppi->type==3){//如果是耗材
                $ss_temp=SupplyClassification::where("id",$_ppi->item_id)->where("status",1)->first();
                if($ss_temp){
                    $array=DataSupplyProductItem::getDefault($car_model_id,$_ppi->item_id);
                    if(!$array){
                        $not_supply_arr[]=$ss_temp->id;
                    }
                }
            }
        }
        return $not_supply_arr;
    }


}